package cn.kgc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.kgc.entity.SmbmsRole;
import cn.kgc.form.SmbmsRoleAddForm;
import cn.kgc.form.SmbmsRoleForm;
import cn.kgc.form.SmbmsRoleUpdateForm;
import cn.kgc.service.SmbmsRoleService;
import cn.kgc.util.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author example
 * @version 1.0
 */
@RestController
@RequestMapping("/smbmsRole")
@Api(tags = "角色管理接口")
@Validated
@Slf4j
@CrossOrigin
@SuppressWarnings("all")
public class SmbmsRoleController {
    @Resource
    private SmbmsRoleService smbmsRoleService;


    @PostMapping("/testGroupValidation1")
    public boolean testGroupValidation1(
        @Validated(value = AddGroup.class) @RequestBody SmbmsRoleForm smbmsRoleForm
    ){
        return true;
    }

    @PostMapping("/testGroupValidation2")
    public boolean updateGroupValidation2(
          @Validated(value = UpdateGroup.class)  @RequestBody SmbmsRoleForm smbmsRoleForm
    ){
        return true;
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有数据")
    @CrossOrigin
    @BaseLog(
        moduleName = ModuleName.ROLE,
        logType = LogType.OPERATE,
        operateType = OperateType.SELECT,
        logContent = "查询所有角色信息"
    )
    public List<SmbmsRole> selectAll() {
         return smbmsRoleService.list();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", defaultValue = "5", dataType = "int", paramType = "query")
    })
    @CrossOrigin
    public IPage<SmbmsRole> selectPage(@RequestParam(defaultValue = "1") int currentPage,
                                       @RequestParam(defaultValue = "5") int pageSize) {
        IPage<SmbmsRole> page = new Page(currentPage, pageSize);

        return smbmsRoleService.page(page);
    }

    @GetMapping("/detail/{id}")
    @CrossOrigin
    @ApiOperation(value = "查看详细信息")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "string")
    public SmbmsRole selectOne(
          @Min(value = 1,message = "角色编码最小值是1") @PathVariable long id
    ) {
        return this.smbmsRoleService.getRoleById(id);
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "string")
    @CrossOrigin
    public boolean delete(@PathVariable String id) {
        return this.smbmsRoleService.removeById(id);
    }


    @PostMapping("/save")
    @ApiOperation(value="新增角色信息")
    public boolean save(@Validated @RequestBody SmbmsRoleAddForm roleAddForm){
        //将新增的数据拷贝到角色对象中
        SmbmsRole smbmsRole=new SmbmsRole();
        BeanUtil.copyProperties(roleAddForm,smbmsRole);
        //新增角色对象
        return smbmsRoleService.save(smbmsRole);
    }

    @PostMapping("/update")
    @ApiOperation(value="修改角色信息")
    public boolean update(SmbmsRoleUpdateForm roleUpdateForm){
        //查询数据库中角色信息
        String id = roleUpdateForm.getId();
        SmbmsRole smbmsRole = smbmsRoleService.getById(id);
        //将修改的数据拷贝到角色对象中
        BeanUtil.copyProperties(roleUpdateForm,smbmsRole);
        //将角色对象信息更新到数据库中
        return smbmsRoleService.updateById(smbmsRole);
    }
}
